﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Warp9
{
    class Controls
    {
        private MouseState previousMouseState;
        private KeyboardState previousKeyboardState;
        private Dictionary<Keys, int> TimedKeys;
        private List<Keys> activeKeys;
        private List<Keys> expiredKeys;
        public Controls()
        {
            TimedKeys = new Dictionary<Keys, int> { };
            activeKeys = new List<Keys> { };
            expiredKeys = new List<Keys> { };
        }

        public void HandleInput(Player player, GameTime gameTime)
        {
            HandleKeyboard(player, gameTime);
            HandleMouse(player, gameTime);
        }

        private void HandleKeyboard(Player player, GameTime gameTime)
        {
            KeyboardState state = Keyboard.GetState();

            if (CheckKey(state, Keys.D1, true, 100))
            {
                player.ResetWorld(-100);
            }
            if (CheckKey(state, Keys.D2, true, 100))
            {
                player.ResetWorld(100);
            }

            if (CheckKey(state, Keys.E))
            {
                player.ErodeWorld(10);
            }
            if (CheckKey(state, Keys.M, true, 100))
            {
                player.CreateMountains(5);
            }
            if (CheckKey(state, Keys.Space, false))
            {

            }


            UpdateTimedKeys(gameTime);

            previousKeyboardState = state;
        }
        private void HandleMouse(Player player, GameTime gameTime)
        {
            MouseState state = Mouse.GetState();



            previousMouseState = state;
        }
        /// <summary>
        /// Simple check for a keypress, checks as often as possible.
        /// </summary>
        /// <param name="state">Keyboard to check</param>
        /// <param name="key">Key to check</param>
        /// <returns></returns>
        private bool CheckKey(KeyboardState state, Keys key)
        {
            if (state.IsKeyDown(key))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Simple check for a keypress, optional repeat toggle
        /// </summary>
        /// <param name="state">Keyboard to check</param>
        /// <param name="key">Key to check</param>
        /// <param name="repeat">Will only check once per press if false</param>
        /// <returns></returns>
        private bool CheckKey(KeyboardState state, Keys key, bool repeat)
        {
            if (!repeat)
            {
                if (CheckKey(state, key) && !CheckKey(previousKeyboardState, key))
                {
                    return true;
                }
            }
            else
            {
                return CheckKey(state, key);
            }
            return false;
        }

        /// <summary>
        /// Advanced check for a keypress, optional repeat toggle, timed recurrance.
        /// </summary>
        /// <param name="state">Keyboard to check</param>
        /// <param name="key">Key to check</param>
        /// <param name="repeat">Will only check once per press if false</param>
        /// <param name="timer">Interval between checks if repeat is true, otherwise a cooldown if repeat is false.</param>
        /// <returns></returns>
        private bool CheckKey(KeyboardState state, Keys key, bool repeat, int timer)
        {
            if (CheckKey(state, key, repeat) & !TimedKeys.ContainsKey(key))
            {
                AddToTimedKeys(key, timer);
                return true;
            }
            return false;
        }

        private void UpdateTimedKeys(GameTime gameTime)
        {
            foreach (Keys key in activeKeys)
            {
                if (TimedKeys.ContainsKey(key))
                {
                    if (TimedKeys[key] > 1)
                    {
                        TimedKeys[key] -= gameTime.ElapsedGameTime.Milliseconds;
                    }
                    else
                    {
                        expiredKeys.Add(key);
                    }
                }
            }
            if (expiredKeys.Count > 0)
            {
                CheckExpiredKeys();
            }
        }

        private void AddToTimedKeys(Keys key, int timer)
        {
            TimedKeys.Add(key, timer);
            activeKeys.Add(key);
        }
        private void CheckExpiredKeys()
        {
            foreach (Keys key in expiredKeys)
            {
                TimedKeys.Remove(key);
                activeKeys.Remove(key);
            }
            expiredKeys.Clear();
        }
    
    }
}
